package com.firewolf.rabbitmq;

import java.io.IOException;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

import utils.ConnectionUtils;

public class Receiver {

	private final static String QUEUE_NAME = "simple_queue";

	public static void main(String[] args) throws Exception {

		Connection connection = ConnectionUtils.getMQConnection(); // 获取连接

		Channel channel = connection.createChannel(); // 创建通道
		channel.queueDeclare(QUEUE_NAME, false, false, false, null); // 声明队列

		Consumer consumer = new DefaultConsumer(channel) { // 创建消费者
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
					byte[] body) throws IOException {
				// 收到消息后会回调这个函数
				String message = new String(body, "UTF-8");
				System.out.println("接收到消息：" + message);
			}
		};
		channel.basicConsume(QUEUE_NAME, true, consumer);
	}
}
